<?php

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Model\PrinterModel;

class PrinterService extends BaseService
{
    public static function getPrinterById(int $id): array
    {
        $couponDetail = PrinterModel::query()->find($id);
        if ($couponDetail == null) {
            throw new ShopException(ShopCodeEnum::ERR_GET_PRINTER,'该打印机不存在');
        }else{
            $couponInfo = $couponDetail->toArray();
            $couponInfo['printerTypeText']     = CodeEnum::PRINTER_TYPE[$couponInfo['printerType']] ?? "";
            $couponInfo['statusText']          = CodeEnum::PRINTER_STATUS[$couponInfo['status']] ?? "";
            $couponInfo['deviceTypeText']      = CodeEnum::PRINTER_DEVICE[$couponInfo['deviceType']] ?? "";
            return $couponInfo;
        }

    }

    /**
     * 添加打印机
     * @param $name
     * @param $shopId
     * @param $brandId
     * @param $printerType
     * @param $deviceType
     * @param $printerIp
     * @param $status
     * @return array
     * @throws ShopException
     */
    public static function addPrinter($name, $shopId, $brandId, $printerType, $deviceType, $printerIp, $status, $deviceId, $printerPort)
    {
        if($deviceType == null){
            $deviceType = CodeEnum::PRINTER_DEVICE_DEFAULT;
        }
        if($status == null){
            $status = CodeEnum::PRINTER_STATUS_NORMAL;
        }
        if($deviceId == null){
            $deviceId = CodeEnum::PRINTER_DEVICE_INDEX;
        }
        if($printerPort == null){
            $printerPort = CodeEnum::PRINTER_PORT;
        }
        return PrinterModel::addData(
            $name,
            (int)$shopId,
            (int)$brandId,
            (int)$deviceId,
            (int)$printerType,
            (int)$deviceType,
            $printerIp,
            (int)$printerPort,
            (int)$status);
    }

    //修改打印机
    public static function setPrinter($id, $name, $printerType, $deviceType, $printerIp, $printerPort, $status, $shopId, $brandId): PrinterModel
    {
        try {
            $model = PrinterModel::where('id', $id)->where('shopId', $shopId)->where('brandId', $brandId)->first();
            if (empty($model)){
                throw new ShopException(ShopCodeEnum::ERR_PRINTER_NOT_FOUND, '打印机配置不存在');
            }
            if ($name != null){
                $model->name = $name;
            }
            if ($printerType != null){
                $model->printerType = $printerType;
            }
            if ($deviceType != null){
                $model->deviceType = $deviceType;
            }
            if ($printerIp != null){
                $model->printerIp = $printerIp;
            }
            if ($printerPort != null){
                $model->printerPort = $printerPort;
            }
            if ($status != null){
                $model->status = $status;
            }
            $model->save();
            return $model;
        } catch (\Exception $exception) {
            throw new ShopException(ShopCodeEnum::ERR_UPDATED_PRINTER, '', $exception);
        }
    }

    //删除打印机
    public static function delPrinter(int $id)
    {
        $query = PrinterModel::query()->find($id);
        if (!$query) {
            throw new ShopException(ShopCodeEnum::ERR_GET_PRINTER, '删除打印机失败（打印机不存在）');
        }

        $s = PrinterModel::delData($id);
        if (!$s) {
            throw new ShopException(ShopCodeEnum::ERR_DEL_PRINTER, '删除打印机失败');
        }else{
            return ['id' => $id];
        }
    }

    public static function getPrinterList($shopId, $brandId)
    {
        $printerList = PrinterModel::query()
            ->where('shopId', $shopId)
            ->where('brandId', $brandId)
            ->get();
        return ['lists' => $printerList->map(function (PrinterModel $printerModel){
            return [
                'id'                  => $printerModel->id,
                'name'                => $printerModel->name,
                'shopId'              => $printerModel->shopId,
                'brandId'             => $printerModel->brandId,
                'deviceId'            => $printerModel->deviceId,
                'printerTypeText'     => CodeEnum::PRINTER_TYPE[$printerModel->printerType] ?? "",
                'printerType'         => $printerModel->printerType,
                'printerIp'           => $printerModel->printerIp,
                'status'              => $printerModel->status,
                'deviceType'          => $printerModel->deviceType,
                'deviceTypeText'      => CodeEnum::PRINTER_DEVICE[$printerModel->deviceType] ?? "",
                'printerPort'         => $printerModel->printerPort,
                'createdAt'           => $printerModel->createdAt->toDateTimeString(),
                'updatedAt'           => $printerModel->updatedAt->toDateTimeString(),
                'createdByUid'        => $printerModel->createdByUid,
                'updatedByUid'        => $printerModel->updatedByUid,
            ];
        })];
    }
}
